package com.wsfk.ssm.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.wsfk.ssm.entity.HandBook;
import com.wsfk.ssm.entity.Picture;
import com.wsfk.ssm.entity.User;
import com.wsfk.ssm.path.CallPath;
import com.wsfk.ssm.path.POPS;
import com.wsfk.ssm.service.PictureSvc;
import com.wsfk.ssm.tools.BaseAction;

@Controller("PictureAction")
public class PictureAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6287015498444751805L;
	private PictureSvc pictureSvc;
	private String httpImage;
	
	
	public String getHttpImage() {
		return httpImage;
	}
	public void setHttpImage(String httpImage) {
		this.httpImage = httpImage;
	}

	private POPS pops;
	
	public POPS getPops() {
		return pops;
	}
	@Resource
	public void setPops(POPS pops) {
		this.pops = pops;
	}

	@Resource
	public void setPictureSvc(PictureSvc pictureSvc) {
		this.pictureSvc = pictureSvc;
	}
	
	private Picture picture;
	
	public Picture getPicture() {
		return picture;
	}

	public void setPicture(Picture picture) {
		this.picture = picture;
	}
	
	private User user;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	private HandBook handBook;
	private List<Picture> listPic;
	
	private List<File> uploadPic;
	private List<String> uploadPicFileName;
	private List<String> place;
	
	public List<String> getPlace() {
		return place;
	}

	public void setPlace(List<String> place) {
		this.place = place;
	}

	//规定
	private File upload;//上传文件 
	//上传文件 的名称
	private String uploadFileName;
	
	//统计有多少张图片
	private int count;
	
	//没有内容图片，返回的提示内容
	private String prompt;

	public String getPrompt() {
		return prompt;
	}

	public void setPrompt(String prompt) {
		this.prompt = prompt;
	}

	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<Picture> getListPic() {
		return listPic;
	}

	
	public void setListPic(List<Picture> listPic) {
		this.listPic = listPic;
	}

	public HandBook getHandBook() {
		return handBook;
	}

	public void setHandBook(HandBook handBook) {
		this.handBook = handBook;
	}

	

	public List<File> getUploadPic() {
		return uploadPic;
	}

	public void setUploadPic(List<File> uploadPic) {
		this.uploadPic = uploadPic;
	}

	public List<String> getUploadPicFileName() {
		return uploadPicFileName;
	}

	public void setUploadPicFileName(List<String> uploadPicFileName) {
		this.uploadPicFileName = uploadPicFileName;
	}

	
	
	//上传内容图片，
	public String saveAllPic(){
		System.out.println(user.getUserName());
		boolean fale = pictureSvc.savePicFile(user, handBook, uploadPic, place);
		if (fale) {
			return "saveallpic";
		} else {
			return "saveallpicfailure";
		}
	}
	
	
	//查看手册图片
	public String showPicture(){
		httpImage = new CallPath().httpPre(pops).build();
		String handBookId = handBook.getId();
		String userId = user.getId();
		
		count = pictureSvc.queryPagePicCountByBookId(handBookId);
		
		if(count == 1){
			handBook = pictureSvc.queryBookById(handBookId);
			user = pictureSvc.queryUserById(userId);
			prompt="您没有上传过内容图片，无法为您显示，";
			
			return "saveallpicture";
		}else{
			//查询出所有的图片 包括icon+content
			listPic =pictureSvc.queryAllPicByBook(handBookId);
			setListPic(listPic);
			return "showpicture";			
		}
	}
	
	
	//修改图片
	public String replacePicture(){
		httpImage = new CallPath().httpPre(pops).build();
		setUser(user);
		setHandBook(handBook);
		setPicture(picture);
		return "replacepicture";
	}


	
	public String updatePicture(){
		if(upload == null){
			return "updatepicturefailure";
		}else{
			//pictureSvc.updatePic(upload, uploadFileName, picture.getFileDir());
			System.out.println(user.getId()+"===========");
			System.out.println(handBook.getId()+"===========");
			String picid = picture.getId();
			System.out.println(picid +"===========");
			pictureSvc.updatePic(user, handBook, picid, upload);
			return "updatepicture";
		}
	}
	
	public String uploadPicture(){
		count = pictureSvc.queryPagePicCountByBookId(handBook.getId());
		return "uploadpicture";
	}
	
	
	public String updateIcon(){
		if(upload == null){
			return "updatepicturefailure";
		}else{
			//pictureSvc.updatePic(upload, uploadFileName, picture.getFileDir());
			System.out.println(user.getId()+"===========");
			System.out.println(handBook.getId()+"===========");
			String picid = picture.getId();
			System.out.println(picid +"===========");
			pictureSvc.updateIcon(user, handBook, picid, upload);
			return "updatepicture";
		}
	}
	
	
	// TODO 修改图标
	public String replaceIcon(){
		httpImage = new CallPath().httpPre(pops).build();
		setUser(user);
		setHandBook(handBook);
		setPicture(picture);
		return "replaceIcon";
	}
	/*
	//添加图片
	public String savePicture(){
		if(upload == null){
			return "updatepicturefailure";
		}else{
			Picture pic = pictureSvc.saveOnePicFile(upload, count, handBook,user.getUserName());
			if(pic != null){
				return "savepicture";
			}else{
				return "updatepicturefailure";			
			}
		}
	}*/

}
